﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.SharePointPeople;

//CID 0657 //2010.12.14 //Begin
namespace InfoJetSoft.Service.Worker
{
    class EXISearchItemsWorker
    {
        public static string searchItems(Form form)
        {
            string ajaxBracket = FormUtil.getAjaxBracket(form);
            
            StringBuilder buffer = new StringBuilder();
            buffer.Append(ajaxBracket);
            string parent = "";
            if (ConfigUtil.UseAJAX())
            {
                parent = "";
            }
            else
            {
                buffer.Append("<script>");
                parent = "parent.";
            }
            buffer.Append(parent + "InfoJet_StopProgress();");
            buffer.Append(parent + "InfoJet_Postbacking = 0;");
            buffer.Append(parent + "InfoJet_OnPostbackEnd();");

            try
            {
                string formId = form.HttpContext.Request.Params["xdoc_param_form_id"];
                string exiId = form.HttpContext.Request.Params["xdoc_param_exi_id"];
                string searchText = form.HttpContext.Request.Params["xdoc_param_exi_search_text"];
                EXIInfo exiInfo = (EXIInfo)form.Manifest.EXIInfoTable[exiId];
                string jsonData = "null";
                jsonData = ReadList(exiInfo, searchText);
                buffer.Append("var InfoJet_EXIItems = ");
                buffer.Append(jsonData);
                buffer.Append(";");
                buffer.Append(parent + "InfoJet_CurrentDialog.EnableFindButton();");
                buffer.Append(parent + "InfoJet_ShowEXIItems( InfoJet_EXIItems );");
            }
            catch (Exception e)
            {
                buffer.Append(parent + "InfoJet_CurrentDialog.EnableFindButton();");
                buffer.Append(parent + "alert(\"" + e.Message + "\");");
            }

            if (!ConfigUtil.UseAJAX())
            {
                buffer.Append("</script>");
            }

            buffer.Append(ajaxBracket);
            return buffer.ToString();
        }

        public static string ReadList(EXIInfo exiInfo, string searchText)
        {
            try
            {
                string dataProviderAssembly = ConfigUtil.ItemPickerDataProviderAssembly();
                Assembly dpAssembly = Assembly.Load(dataProviderAssembly);
                Type dpType = null;
                foreach (Type type in dpAssembly.GetTypes())
                {
                    bool found = false;
                    foreach (Attribute attr in type.GetCustomAttributes(true))
                    {
                        if (attr is DescriptionAttribute)
                        {
                            DescriptionAttribute descAttr = (DescriptionAttribute)attr;
                            string desc = descAttr.Description.Trim();
                            if (desc.Equals("InfoJet ItemPicker, DataProviderClass"))
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                    if (found)
                    {
                        dpType = type;
                    }
                }
                MethodInfo readListMethod = dpType.GetMethod("ReadList", BindingFlags.Public | BindingFlags.Static);
                //CID 0750 //2010.12.29 //Begin
                object[] parameters = { exiInfo.EntityNamespace, exiInfo.EntityName, exiInfo.SystemInstanceName, exiInfo.EntityFinderName, exiInfo.DisplayFieldName, exiInfo.EntityBDCServerURL, exiInfo.MaxResults, searchText };
                //CID 0750 //2010.12.29 //End
                object result = readListMethod.Invoke(null, parameters);
                string jsonData = result as string;
                return jsonData;
            }
            catch (Exception e)
            {
                FileLog.log(e);
                return "null";
            }
        }

        private static string buildTestData()
        {
            int columnCount = 5;
            int rowCount = 50;
            StringBuilder buffer = new StringBuilder();
            buffer.Append("{");
            buffer.Append("'Headers':[");
            for (int i = 0; i < columnCount; i++)
            {
                buffer.Append("{'Name':'Header" + i + "',");
                if (i <= 1)
                {
                    buffer.Append("'IsIdentifierSet':true}");
                }
                else
                {
                    buffer.Append("'IsIdentifierSet':false}");
                }

                if (i < (columnCount - 1))
                {
                    buffer.Append(",");
                }
            }
            buffer.Append("],");
            buffer.Append("'Rows':[");
            for (int j = 0; j < rowCount; j++)
            {
                buffer.Append("{'Fields':[");
                for (int c = 0; c < columnCount; c++)
                {
                    buffer.Append("'Field-" + c + "-" + j + "'");
                    if (c < (columnCount - 1))
                    {
                        buffer.Append(",");
                    }
                }
                buffer.Append("], 'EIR':'eir" + j + "' }");
                if (j < (rowCount - 1))
                {
                    buffer.Append(",");
                }
            }
            buffer.Append("]");
            buffer.Append("}");
            return buffer.ToString();
        }
    }
}
//CID 0657 //2010.12.18 //End